home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / crispy / pfields.bas < prev    next >
BASIC Source File  |  1995-05-09  |  20KB  |  686 lines

  1. ' Crispy1 (Kings on top, Queens on the side, and Jacks in the corners)
  2. ' A diversion by Chris Pando
  3. ' IF IT IS SQUINKY, THEN YOU KNOW IT IS BrilligWare!
  4. '
  5. ' I dedicate this program to the public domain.
  6. '
  7. DefInt A-Z
  8.  
  9. Declare Function CardVersion Lib "VBCards.dll" () As Integer
  10.  
  11. Declare Sub GetCard Lib "VBCards.dll" (ByVal Card As Integer)
  12. Declare Sub GetCardBack Lib "VBCards.dll" (ByVal C As Integer)
  13. Declare Sub GetCardMisc Lib "VBCards.dll" (ByVal C As Integer)
  14.  
  15. Declare Function SameCardValue Lib "VBCards.dll" (ByVal C1 As Integer, ByVal C2 As Integer) As Integer
  16.  
  17. Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
  18. Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
  19.  
  20.  
  21.  
  22. Const TRUE = -1
  23. Const FALSE = 0
  24. Const PIXEL = 3
  25. Const ROYALMOD = 13
  26. Const KING = 0
  27. Const QUEEN = 12
  28. Const JACK = 11
  29.  
  30. Const KING1 = 1   ' valid squares for face cards
  31. Const KING2 = 2
  32. Const KING3 = 13
  33. Const KING4 = 14
  34. Const QUEEN1 = 4
  35. Const QUEEN2 = 7
  36. Const QUEEN3 = 8
  37. Const QUEEN4 = 11
  38. Const JACK1 = 0
  39. Const JACK2 = 3
  40. Const JACK3 = 12
  41. Const JACK4 = 15
  42.  
  43. Const CENTER1 = 5   'symbolic values for the center squares
  44. Const CENTER2 = 6
  45. Const CENTER3 = 9
  46. Const CENTER4 = 10
  47.  
  48. Const CLOSEDFIELD = 1   ' here are the various states
  49. Const OPENFIELD = 2
  50. Const APENDING = 3
  51. Const CPENDING = 4
  52.  
  53. Dim Deck(1 To 52) As Integer    'Deck contains the deal
  54. Dim Field(15) As Integer        'Field contains the playing field
  55.  
  56.             ' State Variables
  57.  
  58. Dim FromCard As Integer         'Contains Card to Match
  59. Dim NumberOpen As Integer       'open places on field
  60. Dim NextCard As Integer         'Pointer to Deck
  61. Dim State As Integer            'Current State
  62. Dim BackDesign As Integer       'Card Back
  63.  
  64. Sub AppExit ()
  65.    WriteProfile
  66.    End
  67. End Sub
  68.  
  69. Sub CenterCtlBottom (Source As Form, Ctl As Control, Alt As Integer)
  70.  ' center control a specified distance above the bottom of the form
  71.  ' works regardless of ScaleMode
  72.  
  73.    Ctl.Top = Source.ScaleHeight - Ctl.Height - Alt
  74.    Ctl.Left = (Source.ScaleWidth - Ctl.Width) / 2
  75. End Sub
  76.  
  77. Sub ChangeCardBack (Index As Integer)
  78.     ReverseImage CardBack.CardBackPic(BackDesign - 1)
  79.     ReverseImage CardBack.CardBackPic(Index)
  80.     BackDesign = Index + 1
  81.     If (NextCard <> 53) Then
  82.        Pfield.NewCards.Picture = CardBack.CardBackPic(Index).Picture
  83.     End If
  84. End Sub
  85.  
  86. Sub CtlEnable (SetMe As Integer)
  87. ' 1 = Click2 off, menus off
  88. ' 2 = Click2 off, menus on
  89. ' 3 = Click2 on, menus on
  90.  
  91.    Select Case SetMe
  92.       Case 1
  93.          Pfield.Click2.Enabled = False
  94.          Pfield.CardBacks.Enabled = False
  95.          Pfield.Shelp.Enabled = False
  96.          Pfield.CAbout.Enabled = False
  97.       Case 2
  98.          Pfield.Click2.Enabled = False
  99.          Pfield.CardBacks.Enabled = True
  100.          Pfield.Shelp.Enabled = True
  101.          Pfield.CAbout.Enabled = True
  102.       Case 3
  103.          Pfield.Click2.Enabled = True
  104.          Pfield.CardBacks.Enabled = True
  105.          Pfield.Shelp.Enabled = True
  106.          Pfield.CAbout.Enabled = True
  107.    End Select
  108.  
  109. End Sub
  110.  
  111. Sub DealCards ()
  112. ' deal a card to all empty positions on the playing field
  113.    i = 0
  114.    Do While (NextCard < 53 And NumberOpen > 0) 'NumberOpen > 0 implies that
  115.       Do While (Field(i) <> -1)                ' there exists a Field(I)
  116.          i = i + 1                             ' = -1
  117.       Loop
  118.  
  119.       Field(i) = Deck(NextCard)
  120.       GetCard (Field(i))
  121.       Pfield.Picture1(i).Picture = ClipBoard.GetData(2)
  122.       NumberOpen = NumberOpen - 1
  123.       NextCard = NextCard + 1
  124.       If (NextCard = 53) Then
  125.          GetCardMisc (1)
  126.       Else
  127.          GetCardBack (BackDesign)
  128.       End If
  129.       Pfield.NewCards.Picture = ClipBoard.GetData(2)
  130.     
  131.       State = CLOSEDFIELD      'set state
  132.       CtlEnable 2
  133.    Loop
  134. End Sub
  135.  
  136. Sub DealDeck ()
  137.  
  138.    NextCard = 1
  139.    For i = 0 To 15
  140.       Field(i) = Deck(NextCard)
  141.       NextCard = NextCard + 1
  142.       GetCard (Field(i))
  143.       Pfield.Picture1(i).Picture = ClipBoard.GetData(2)
  144.    Next
  145.  
  146.    GetCardBack (BackDesign)
  147.    Pfield.NewCards.Picture = ClipBoard.GetData(2)
  148.    
  149.    GetCardMisc (1)
  150.    Pfield.OldCards.Picture = ClipBoard.GetData(2)
  151.     
  152.    CtlEnable 2
  153.    NumberOpen = 0
  154.    State = CLOSEDFIELD
  155.  
  156. End Sub
  157.  
  158. Sub Engine (Index As Integer)
  159.     ' This is the routine that does all the work
  160.     ' I decided this routine would be most reliably implemented
  161.     ' as a semi-rigorous finite state machine
  162.     '
  163.     '
  164.     '
  165.     Select Case State
  166.        Case CLOSEDFIELD
  167.  
  168.           If (RoyalCard(Index) <> True) Then
  169.              ReverseImage Pfield.Picture1(Index)
  170.              State = APENDING
  171.              CtlEnable 1
  172.              FromCard = Index
  173.           End If
  174.  
  175.  
  176.        Case OPENFIELD
  177.  
  178.           If (Field(Index) <> -1) Then
  179.              If (RoyalCard(Index) <> True) Then
  180.                 ReverseImage Pfield.Picture1(Index)
  181.                 State = APENDING
  182.                 CtlEnable 1
  183.                 FromCard = Index
  184.              Else
  185.                 ReverseImage Pfield.Picture1(Index)
  186.                 State = CPENDING
  187.                 CtlEnable 1
  188.                 FromCard = Index
  189.              End If
  190.           End If
  191.  
  192.        Case APENDING
  193.           If (FromCard = Index) Then
  194.              ReverseImage Pfield.Picture1(Index)
  195.              OpenOrClosed                        ' set state
  196.           ElseIf (SameCardValue(Field(FromCard), Field(Index))) Then
  197.              Remove FromCard, Index
  198.              OpenOrClosed                        ' set state
  199.           End If
  200.  
  201.        Case CPENDING
  202.  
  203.           If (FromCard = Index) Then
  204.              ReverseImage Pfield.Picture1(Index)
  205.              State = OPENFIELD
  206.              CtlEnable 3
  207.           ElseIf (ValidEmpty(Index)) Then
  208.              Swap FromCard, Index
  209.              State = OPENFIELD
  210.              CtlEnable 3
  211.           End If
  212.  
  213.     End Select
  214.  
  215.     If TestWin() Then
  216.       LoadWin
  217.     End If
  218. End Sub
  219.  
  220. Sub FrameForm (Source As Form)
  221.    Source.Line (0, 0)-(Source.ScaleWidth - 1, Source.ScaleHeight - 1), RGB(0, 0, 0), B
  222.    Source.Line (1, 1)-(Source.ScaleWidth - 2, Source.ScaleHeight - 2), RGB(255, 255, 255), B
  223.    Source.Line (4, 4)-(Source.ScaleWidth - 5, Source.ScaleHeight - 5), RGB(128, 128, 128), B
  224. End Sub
  225.  
  226. Sub GetProfile ()
  227. ' set a couple of the global variables
  228.    Pfield.WindowState = GetPrivateProfileInt("Crispy1", "WindowState", 2, "CRISPY.INI")
  229.    BackDesign = GetPrivateProfileInt("Crispy1", "BackDesign", 1, "CRISPY.INI")
  230. End Sub
  231.  
  232. Sub Init ()
  233.    GetProfile
  234.    InitDeck
  235.    ShuffleDeck
  236.    DealDeck
  237. End Sub
  238.  
  239. Sub InitDeck ()
  240. ' randomize, load cards into array, and condition control array
  241.    Randomize
  242.  
  243.    For i = 1 To 52
  244.       Deck(i) = i
  245.    Next i
  246.  
  247.    For i = 0 To 15
  248.       Pfield.Picture1(i).FillStyle = 0                   'solid
  249.       Pfield.Picture1(i).FillColor = RGB(192, 192, 192)  'gray
  250.       Pfield.Picture1(i).DrawMode = 10           ' NOT XOR
  251.       Pfield.Picture1(i).ScaleMode = PIXEL
  252.       Pfield.Picture1(i).ScaleHeight = 96
  253.       Pfield.Picture1(i).ScaleWidth = 71
  254.    Next i
  255. End Sub
  256.  
  257. Sub LoadAbout ()
  258.    Dim Color1 As Long
  259.    Dim Color2 As Long
  260.  
  261.    Color2 = RGB(255, 255, 255)
  262.    Color1 = RGB(128, 128, 128)
  263.  
  264.    Load Form1
  265.  
  266.    Form1.WindowState = 0
  267.    Form1.AutoRedraw = True
  268.    Form1.ScaleMode = PIXEL
  269.    Form1.BackColor = RGB(192, 192, 192)
  270.   
  271.    Form1.Top = 690
  272.    Form1.Left = 480
  273.    Form1.Height = 6135
  274.    Form1.Width = 5055
  275.  
  276. ' position the command botton
  277.    CenterCtlBottom Form1, Form1.Command1, 10
  278.    
  279. ' go ahead and frame the form
  280.    FrameForm Form1
  281.  
  282. ' now lets print something
  283.    Form1.FontName = "Helv"
  284.    Form1.FontSize = 24
  285.  
  286.    Form1.ForeColor = Color2
  287.    Form1.CurrentX = 85
  288.    Form1.CurrentY = 15
  289.    Form1.Print "Crispy1"
  290.  
  291.    Form1.ForeColor = Color1
  292.    Form1.CurrentX = 84
  293.    Form1.CurrentY = 14
  294.    Form1.Print "Crispy1"
  295.  
  296.    Form1.ForeColor = Color1
  297.    Form1